package com.hezhou.jenkins.util;

import com.hezhou.jenkins.my_jenkins_test.entity.SysUser;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * ---------------------------
 * (SysUserPOIUtil) 用户表poi excel导出
 * ---------------------------
 *
 * @Author: [hezhou]
 * @Date: 2020/5/7
 * @Version: [1.0.1]
 * ---------------------------
 */
public class SysUserPOIUtil {


    private static List<String> CELL_HEADS; //列头

    static {
        // 类装载时就载入指定好的列头信息，如有需要，可以考虑做成动态生成的列头
        CELL_HEADS = new ArrayList<>();
        CELL_HEADS.add("id");
        CELL_HEADS.add("名字");
        CELL_HEADS.add("昵称");
        CELL_HEADS.add("电话");
        CELL_HEADS.add("电子邮箱");
        CELL_HEADS.add("是否可用");
        CELL_HEADS.add("创建时间");
        CELL_HEADS.add("修改时间");
    }


    /**
     * 生成Excel并写入数据信息
     * @param dataList 数据列表
     * @return 写入数据后的工作簿对象
     */
    public static Workbook exportData(List<SysUser> dataList){
        // 生成xlsx的Excel
        Workbook workbook = new SXSSFWorkbook();

        // 如需生成xls的Excel，请使用下面的工作簿对象，注意后续输出时文件后缀名也需更改为xls
        //Workbook workbook = new HSSFWorkbook();

        // 生成Sheet表，写入第一行的列头
        Sheet sheet = buildDataSheet(workbook);
        //构建每行的数据内容
        int rowNum = 1;
        for (Iterator<SysUser> it = dataList.iterator(); it.hasNext(); ) {
            SysUser data = it.next();
            if (data == null) {
                continue;
            }
            //输出行数据
            Row row = sheet.createRow(rowNum++);
            convertDataToRow(data, row);
        }
        return workbook;
    }

    /**
     * 将数据转换成行
     * @param user 源数据
     * @param row 行对象
     * @return
     */
    private static void convertDataToRow(SysUser user, Row row){
        int cellNum = 0;
        Cell cell;
        // id
        cell = row.createCell(cellNum++);
        if (null != user.getId()) {
            cell.setCellValue(user.getId());
        } else {
            cell.setCellValue("");
        }
        // 姓名
        cell = row.createCell(cellNum++);
        cell.setCellValue(null == user.getUsername() ? "" : user.getUsername());

        // 昵称
        cell = row.createCell(cellNum++);
        cell.setCellValue(null == user.getNickName() ? "" : user.getNickName());
        // 电话
        cell = row.createCell(cellNum++);
        cell.setCellValue(null == user.getMobile() ? "" : user.getMobile());

        // 电子邮箱
        cell = row.createCell(cellNum++);
        cell.setCellValue(null == user.getEmail() ? "" : user.getEmail());

        // 是否可用
        cell = row.createCell(cellNum++);
        cell.setCellValue(0 == user.getIsEnabled() ? "不可用":"可用");

        // 创建时间
        cell = row.createCell(cellNum++);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String create = sdf.format(user.getCreateDate());
        cell.setCellValue(null == create ? "" :create);
        // 修改时间
        cell = row.createCell(cellNum++);
        String update = sdf.format(user.getCreateDate());
        cell.setCellValue(null == update ? "" : update);
    }

    /**
     * 生成sheet表，并写入第一行数据（列头）
     * @param workbook 工作簿对象
     * @return 已经写入列头的Sheet
     */
    private static Sheet buildDataSheet(Workbook workbook) {
        Sheet sheet = workbook.createSheet("管理员统计报表");
        // 设置列头宽度
        for (int i=0; i<CELL_HEADS.size(); i++) {
            sheet.setColumnWidth(i, 6000);
            sheet.setDefaultRowHeight((short)600);
        }
        // 构建头单元格样式
        CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
        // 写入第一行各列的数据
        Row head = sheet.createRow(0);
        for (int i = 0; i < CELL_HEADS.size(); i++) {
            Cell cell = head.createCell(i);
            cell.setCellValue(CELL_HEADS.get(i));
            cell.setCellStyle(cellStyle);
        }
        return sheet;
    }

    /**
     * 设置第一行列头的样式
     * @param workbook 工作簿对象
     * @return 单元格样式对象
     */
    private static CellStyle buildHeadCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        //对齐方式设置
        style.setAlignment(HorizontalAlignment.CENTER);
        //边框颜色和宽度设置
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框
        //设置背景颜色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //粗体字设置
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }

}
